package com.guojizhuang.APIdemo;

import com.guojizhuang.util.MonthJson;
import com.guojizhuang.util.testJson;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;

public class HadoopAPIDemo {
    static
    PhoneServiceImpl phoneService;
    public static void main(String[] args) throws IOException {


        MonthJson.readFromHdfs("hdfs://niit-master:9000/user/niit/output/guo/partition/part-r-00004");
    }

    private static void writeIntoLocal(FileSystem fileSystem, String hdfsPath, String localPath) throws IOException {
        FSDataInputStream in = null;
        FileOutputStream outputStream = null;
        try {
            in = fileSystem.open( new Path(hdfsPath));

            outputStream=new FileOutputStream(localPath);
            //          从输入流复制字节数组到（系统默认）输出流
            //          每个批次的大小，是否执行完复制动作后关闭输入和输出流
            IOUtils.copyBytes(in, outputStream, 4096, false);
        } finally {
            IOUtils.closeStream(in);
            IOUtils.closeStream(outputStream);
        }
    }

    private static void writeintoHdfs(FileSystem fileSystem, Path dfs) throws IOException {
        byte[] file_content_buff=generateMultiplicationTable().getBytes();

// create方法是创建了一个可以用于写出数据（字节数组）的Hadoop定制过的输出流对象
        FSDataOutputStream outputStream= fileSystem.create(dfs);
// 流的写出，基本操作都是：把哪个字节数组的从第几个字节到第几个字节写出到输出流
        outputStream.write(file_content_buff,  0,      file_content_buff.length);
        outputStream.close();
    }

    private static void writeFile(String[] args,String path) throws IOException {
        byte[] file_buff=generateMultiplicationTable().getBytes();
        FileSystem fileSystem = FileSystem.get(URI.create(args[0]),new Configuration());
        FSDataOutputStream fsDataOutputStream= fileSystem.create(new Path(path));
        fsDataOutputStream.write(file_buff,0,file_buff.length);
        fsDataOutputStream.writeUTF("hello,world");
        fsDataOutputStream.close();
    }

    private static void listOut(String args, FileSystem fileSystem) throws IOException {
        FileStatus[] fileStatuses = fileSystem.listStatus(new Path(args));
        for (FileStatus fileStatus:fileStatuses)
        {
            System.out.println(fileStatus.getPath().toString());
        }
    }



    private static void readFromHdfs(String hdfsUri) throws IOException {
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(URI.create(hdfsUri), conf);
        FSDataInputStream in = null;
        BufferedReader reader = null;
        try {
            in = fs.open(new Path(hdfsUri));
            reader = new BufferedReader(new InputStreamReader(in, "UTF-8")); // 使用UTF-8编码

            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } finally {
            if (reader != null) {
                reader.close();
            }
            if (in != null) {
                in.close();
            }
        }
    }

    public static String generateMultiplicationTable() {
        StringBuilder table = new StringBuilder();

        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                table.append(j).append(" x ").append(i).append(" = ").append(i * j).append("\t");
            }
            table.append("\n");
        }

        return table.toString();
    }

    private static void mkdir(String[] args) throws IOException {
        //判断传入参数的个数
        if (args.length == 0) {
            System.out.println("请输出执行的参数");
            System.exit(0);
        }
        String uri = args[0];
        Configuration configuration = new Configuration();
        FileSystem fileSystem = FileSystem.get(URI.create(uri), configuration);


        //{/home/TestDir}
        Boolean result = fileSystem.mkdirs(new Path(uri));

        if (result) {
            System.out.println("创建成功");
        } else {
            System.out.println("创建失败");
        }
    }
}
